package cn.com.headfree.competent.controller;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.com.headfree.competent.service.IUserDataInfoService;
import cn.com.headfree.entity.TProTecInfo;
import cn.com.headfree.entity.TSeniorCompetent;
import cn.com.headfree.vo.HonorDatasVo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gta.edu.sdk.common.dto.Page;
import com.gta.edu.sdk.util.StringUtil;
import cn.com.headfree.base.controller.ExBaseController;
import cn.com.headfree.base.controller.ResultData;
import cn.com.headfree.entity.TOrgDept;
import cn.com.headfree.entity.isc.User;
import cn.com.headfree.competent.service.ISeniorService;

/**
 * 技能等级申报
 */
@Controller
@RequestMapping(value = "/senior", produces = "application/json;charset=UTF-8")
public class SeniorController extends ExBaseController {

	private static final Logger LOG = LogManager.getLogger(SeniorController.class);

	@Autowired
	private ISeniorService seniorService;

	@Autowired
	private IUserDataInfoService userDataInfoService;

	@RequestMapping("/addSenior")
	@ResponseBody
	public ResultData addSenior(TSeniorCompetent senior, HttpServletRequest request) {
		ResultData rs = new ResultData();
		try {
			User user = this.getLoginUserInfo(request);
			TSeniorCompetent entity = seniorService.judge(senior, user);
			if (entity != null&&!StringUtil.isNullOrEmpty(entity.getId())) {
				rs.setSuccess(false);
				rs.setErrorMsg("当前年度已申报该专业资格");
			} else {
				// 添加技能等级申报
				seniorService.addSenior(senior, user);
				rs.setData("职称申报创建成功！");
				LOG.info("职称申报创建成功！");
			}
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("职称申报创建失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 分页查询职称申报
	 * 
	 * @param page
	 * @param tSeniorCompetent
	 * @return
	 */
	@RequestMapping("/getPageSeniorTechnician")
	@ResponseBody
	public ResultData getPageSeniorCompetent(Page<TSeniorCompetent> page, TSeniorCompetent tSeniorCompetent,
			HttpServletRequest request) {
		ResultData rs = new ResultData();
		try {
			User user = this.getLoginUserInfo(request);
			seniorService.getPageSeniorCompetent(page, tSeniorCompetent, user);
			rs.setData(page);
			LOG.info("查找职称申报成功");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("查找职称申报失败！");
			e.printStackTrace();
		}
		return rs;
	}

	/**
	 * 查询登录人名称及单位
	 * @param request
	 * @return
	 */
	@RequestMapping("/addSeniorPre")
	@ResponseBody
	public ResultData addSeniorPre(HttpServletRequest request) {
		ResultData rs = new ResultData();
		try {
			User user = this.getLoginUserInfo(request);
			TOrgDept dept = seniorService.addSeniorPre(user);
			List<HonorDatasVo> tProTecInfos = userDataInfoService.getProTecInfoByErpNo(user.getFax());
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("userName", user.getName());
			map.put("orgName", dept.getName());
			map.put("erpNo", user.getFax());
			String tecName = null;
			Date acquTime = null;
			String auditUnit = null;
			String certNo = null;
			if(tProTecInfos != null && tProTecInfos.size() > 0){
				tecName = tProTecInfos.get(0).getHonorName();
				acquTime = tProTecInfos.get(0).getAcquTime();
				auditUnit = tProTecInfos.get(0).getAwardUnit();
				certNo = tProTecInfos.get(0).getCertNo();
			}
			map.put("tecName",tecName);
			map.put("acquTime",acquTime);
			map.put("auditUnit",auditUnit);
			map.put("certNo",certNo);
			rs.setData(map);
			LOG.info("获取部门成功！");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("获取部门失败！");
			e.printStackTrace();
		}
		return rs;
	}

	@RequestMapping("/downloadAllFiles")
	public void downloadAllFiles(HttpServletRequest request, HttpServletResponse response, String sysCode,
								 String businessNode, String seniorId, Date acquTime) {
		ResultData rs = new ResultData();
		try {
			User user = this.getLoginUserInfo(request);
			seniorService.downloadFiles(user.getFax(), sysCode, businessNode, request, response,seniorId,acquTime);
			LOG.info("下载附件成功！");
		} catch (Exception e) {
			rs.setSuccess(false);
			rs.setErrorMsg("下载附件失败！");
			e.printStackTrace();
		}
	}

}
